package com.erpanderp.wam.asset.model.service;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.FlushMode;
import org.hibernate.Query;
import org.hibernate.Session;

import com.erpanderp.wam.asset.model.entity.AssetMaster;
import com.erpanderp.wam.util.HibernateUtil;

public class AssetMasterDAO {
	 
	public AssetMaster saveOrUpdateAssetMaster(AssetMaster assetMaster) {
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.getTransaction().begin();
			System.out.println("FLUSH MODE: " + session.getFlushMode());
			session.setFlushMode(FlushMode.COMMIT);
			session.saveOrUpdate(assetMaster);
			session.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return assetMaster;
	}
	
	public List<AssetMaster> getAllAssetsByAssetStatus(String assetType,String assetStatus){
		List<AssetMaster> assets = new ArrayList<AssetMaster>();
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			String queryString = "from AssetMaster am where am.assetType =:assetType and am.assetStatus=:assetStatus";
			Query query = session.createQuery(queryString);
			query.setCacheable(false).setString("assetStatus", assetStatus).setString("assetType", assetType);			
			assets =(List<AssetMaster>) query.list();
			session.getTransaction().commit();
			return assets;	
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}		
	}
	
	public List<AssetMaster> getAllAssets(String assetType){
		List<AssetMaster> assets = new ArrayList<AssetMaster>();
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			String queryString = "from AssetMaster am where am.assetType=:assetType";
			Query query = session.createQuery(queryString);
			query.setCacheable(false).setString("assetType", assetType);			
			assets =(List<AssetMaster>) query.list();
			session.getTransaction().commit();
			return assets;	
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}		
	}
	
	public boolean isAssetCodeExists(String assetCode) {
		boolean  assetCodeExists=true;
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			Query query = session.createQuery("from AssetMaster am where am.assetCode=:assetCode").setString(
					"assetCode", assetCode);
			query.setCacheable(false);
			List<AssetMaster> tempList = query.list();
			session.getTransaction().commit();				
			if(tempList != null && tempList.size()>0){
				return assetCodeExists;
			}else{
				assetCodeExists=false;
				return assetCodeExists;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return assetCodeExists;
		}		
	}
	
	public AssetMaster getAssetMasterByAssetCode(String assetCode) {
		AssetMaster  assetMaster;
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			Query query = session.createQuery("from AssetMaster am where am.assetCode=:assetCode").setString(
					"assetCode", assetCode);
			query.setCacheable(false);
			List<AssetMaster> tempList = query.list();
			session.getTransaction().commit();				
			if(tempList != null && tempList.size()>0){
				assetMaster = tempList.get(0);
				return assetMaster;
			}else{
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}		
	}

}
